package com.xindacloud.sms.console.system.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.console.result.Result;
import com.xindacloud.sms.console.result.ResultGenerator;
import com.xindacloud.sms.console.system.dto.SmsSystemMenuDTO;
import com.xindacloud.sms.console.system.entity.SmsMenuRole;
import com.xindacloud.sms.console.system.entity.SmsSystemMenu;
import com.xindacloud.sms.console.system.entity.SmsUserRole;
import com.xindacloud.sms.console.system.mapper.SmsMenuRoleMapper;
import com.xindacloud.sms.console.system.mapper.SmsSystemMenuMapper;
import com.xindacloud.sms.console.system.mapper.SmsUserRoleMapper;
import com.xindacloud.sms.console.system.service.ISmsSystemMenuService;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author czc
 * @since 2019-10-08
 */
@Service
public class SmsSystemMenuServiceImpl extends ServiceImpl<SmsSystemMenuMapper, SmsSystemMenu> implements ISmsSystemMenuService {

	@Override
	public Result getSmsSystemMenuList(SmsSystemMenuDTO smsSystemMenuDTO) {
			SmsSystemMenuDTO ssdto=new SmsSystemMenuDTO();
			ssdto.setId(0);
			getSystemMenus(ssdto);
			return ResultGenerator.genSuccessResult(ssdto.getChildren());
		
	}
	
	private SmsSystemMenuDTO getSystemMenus(SmsSystemMenuDTO menuDto) {
		List<SmsSystemMenuDTO> dtoList=new ArrayList<>();
		QueryWrapper<SmsSystemMenu> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("parent_id", menuDto.getId());
		List<SmsSystemMenu> menusList=this.list(queryWrapper);
		if(menusList!=null&& menusList.size()>0) {
		for(int i=0;i<menusList.size();i++) {
			SmsSystemMenuDTO dto2=new SmsSystemMenuDTO();
			BeanUtils.copyProperties(menusList.get(i),dto2);
			if(menuDto.getId()==0) {
				dto2.setParentId(null);
			}
			dtoList.add(dto2);
		}
		menuDto.setChildren(dtoList);
		}
		if(menusList!=null && menusList.size()>0) {
		getMenuList(menuDto);
		}
		return menuDto;
	}

	private void getMenuList(SmsSystemMenuDTO menuDto) {
		for(int i=0;i<menuDto.getChildren().size();i++) {
			getSystemMenus(menuDto.getChildren().get(i));
		}
	}

	@Override
	public Result saveOrUpdateSmsSystemMenu(SmsSystemMenuDTO smsSystemMenuDTO) {
		SmsSystemMenu smsSystemMenu=new SmsSystemMenu();
		try {
			BeanUtils.copyProperties(smsSystemMenuDTO,smsSystemMenu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(smsSystemMenu.getParentId()==null) {
			smsSystemMenu.setParentId(0);
		}
		if(this.saveOrUpdate(smsSystemMenu)){
		    return ResultGenerator.genSuccessResult("保存或修改成功！");
		}else {
			return ResultGenerator.genClientFailResult("保存或修改失败！");
		}
	}

	@Override
	public Result deleteSmsSystemMenu(SmsSystemMenuDTO smsSystemMenuDTO) {
		if(this.removeById(smsSystemMenuDTO.getId())){
		    return ResultGenerator.genSuccessResult("删除成功！");
		}else {
			return ResultGenerator.genClientFailResult("删除失败！");
		}
	}

	 @Autowired
	 private SmsUserRoleMapper smsUserRoleMapper;
	 @Autowired
	 private SmsMenuRoleMapper smsMenuRoleMapper;
	 @Autowired
	 private SmsSystemMenuMapper smsSystemMenuMapper;
	@Override
	public Result getSmsUserMenuList(Integer userId) {
		QueryWrapper<SmsUserRole> queryRoleWrapper=new QueryWrapper<>();
		queryRoleWrapper.eq("user_id", userId);
		List<SmsUserRole> surList=smsUserRoleMapper.selectList(queryRoleWrapper);
		Set<SmsSystemMenu> ssmSet=new HashSet<>();
		for(SmsUserRole smsUserRole:surList) {
			QueryWrapper<SmsMenuRole> queryMenuWrapper=new QueryWrapper<>();
			queryMenuWrapper.eq("role_id", smsUserRole.getRoleId());
			List<SmsMenuRole> smrList=smsMenuRoleMapper.selectList(queryMenuWrapper);
			for(SmsMenuRole smsMenuRole:smrList) {
				SmsSystemMenu smsSystemMenu=smsSystemMenuMapper.selectById(smsMenuRole.getMenuId());
				if(smsSystemMenu!=null) {
				ssmSet.add(smsSystemMenu);
				}
			}
		}
		
		return ResultGenerator.genSuccessResult(ssmSet);
	}

}
